package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.model.Entity.ScenicSpot;
import org.yty.travelagencymongodb.service.ScenicSpotService;

import java.util.List;

@RestController
@RequestMapping("scenic-spot")
@RequiredArgsConstructor
public class ScenicSpotController {
    private final ScenicSpotService scenicSpotService;

    @GetMapping("/all")
    public Result<List<ScenicSpot>> getAllScenicSpot() {
        List<ScenicSpot> list = this.scenicSpotService.list();
        if (list == null || list.size() == 0){
            return Result.error("暂无数据");
        }
        return Result.ok(list);
    }

    @GetMapping("/{id}")
    public Result<ScenicSpot> getScenicSpotById(@PathVariable Long id) {
        ScenicSpot scenicSpot = this.scenicSpotService.getById(id);
        if (scenicSpot == null){
            return Result.error("暂无数据");
        }
        return Result.ok(scenicSpot);
    }

    @PutMapping
    public Result<Void> updateScenicSpot(@RequestBody ScenicSpot scenicSpot) {
        boolean success = this.scenicSpotService.update(scenicSpot);
        if (!success){
            return Result.error("更新失败");
        }
        return Result.ok();
    }

    @PostMapping
    public Result<Void> saveScenicSpot(@RequestBody ScenicSpot scenicSpot) {
        boolean success = this.scenicSpotService.save(scenicSpot);
        if (!success){
            return Result.error("保存失败");
        }
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteScenicSpotById(@PathVariable Long id) {
        this.scenicSpotService.removeById(id);
        return Result.ok();
    }


}
